<template>
	<view style="padding-bottom: 140rpx;">
		<view class="mainpadding2">
			<u-tabs :list="tabList" :scrollable="false" @click="tabchange" lineWidth="60rpx"
				lineColor="rgba(255, 184, 0, 1)" :activeStyle="{
			  color: '#000000',
			  fontWeight: 800,
			  transform: 'scale(1.05)'}" :inactiveStyle="{
			  color: '#666666',
			    transform: 'scale(1)'
			}"></u-tabs>
			<!-- 訊息 -->
			<template v-if="tabsid==0">
				<view class="chatItemMessage margin_top2" v-for="(item,index) in chatList" :key="item.id" @click="enterchat(item)">
					<view class="imageBox">
						<image :src="item.firend.avatar_text" mode="">
						</image>
					</view>
					<view class="right">
						<view class="nameTime">
							<view class="flexleft">
								<view class="name xiankuan">{{item.firend.nickname}}</view>
								<view class="number" v-if="item.noread_num">{{item.noread_num}}</view>
							</view>
							<view class="time xiaohei" v-if="item.new_content">{{item.new_content.createtime_text}}</view>
						</view>
						<template v-if="item.new_content && item.new_content.msg_status==1">
							<view class="bottom  yihang" v-if="item.new_content.data_type==1">{{item.new_content.message}}</view>
							<view class="bottom yihang" v-if="item.new_content.data_type==2">[圖片]</view>
							<view class="bottom yihang" v-if="item.new_content.data_type==3">[影片]</view>
						</template>
						<template v-if="item.new_content && item.new_content.msg_status==2">
							<view class="bottom yihang" >{{item.new_content.sender_id=='shop'+user_id?'你':'對方'}}撤回了一條消息</view>
						</template>
					</view>
				</view>
			</template>
			<!-- 公告 || 活動 -->
			<template v-if="tabsid==1 || tabsid==2">
				<view class="mainpadding_top ffffff border2" v-for="item in list.data" :key="item.id"
					@click="jumpdetail('/pages/message/noticeDetail?id=',item.id)">
					<view class="flexbetween">
						<view class="stateBox text_e radius color2">{{item.title}}</view>
						<view class="color_six text_five margin_top2">{{item.createtime_text}}</view>
					</view>
					<view class="margin_top color_ling text_a padding_bottom border_bottom">
						{{item.des}}
					</view>
					<view class="flexright margin_top2">
						<view class="lanlable">看詳情</view>
					</view>
				</view>
			</template>
			<!-- 調查 -->
			<template v-if="tabsid==3">
				<view class="mainpadding_top radius shadow" style="background-color: #fbf6f1;" v-for="item in list.data"
					@click="jumpdetail('/pages/message/diaochaxq?id=',item.id)">
					<view class="flexbetween pdl" style="padding-bottom: 10rpx;">
						<view class="flexleft ">
							<view class="margin_right2">
								<image :src="item.image_text" mode="" class="radius"
									style="width: 120rpx;height: 120rpx;min-width: 120rpx;"></image>
							</view>
							<view class="line margin_right2"></view>
							<view>
								<view class="hei_text titletext bold" style="word-break: break-all;">{{item.name}}</view>
								<view class="flexright margin_top1" v-if="item.orderinfo && item.orderinfo.status!=1 && item.orderinfo.status!=4">
									<view class="huang_text main_size bold " v-show="item.orderinfo.payinfo.paytype==1">
										點數支付
									</view>
									<view class="huang_text main_size bold " v-show="item.orderinfo.payinfo.paytype==2">多元支付
									</view>
									<view class="huang_text main_size bold " v-show="item.orderinfo.payinfo.paytype==3">現金支付
									</view>
									<view class="huang_text main_size bold " v-show="item.orderinfo.payinfo.paytype==4">信用卡
									</view>
									<view class="huang_text main_size bold " v-show="item.orderinfo.payinfo.paytype==5">Line Pay線上支付
									</view>
									<view class="huang_text main_size bold " v-show="item.orderinfo.payinfo.paytype==6">街口線上支付
									</view>
									<view class="huang_text main_size bold " v-show="item.orderinfo.payinfo.paytype==7">街口線下支付
									</view>
									<view class="huang_text main_size bold " v-show="item.orderinfo.payinfo.paytype==8">Line Pay線下支付
									</view>
									<view class="huang_text main_size bold " v-show="item.orderinfo.payinfo.paytype==9">Line Pay二維碼支付
									</view>
									<view class="huang_text main_size bold " v-show="item.orderinfo.payinfo.paytype==10">街口二維碼支付
									</view>
									<view class="huang_text main_size bold " v-show="item.orderinfo.payinfo.paytype==11">全支付線下支付
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="flexleft margin_top1">
						<view class="hui_text bold" style="font-size: 26rpx;">活動時間</view>
						<view class="hong_text bold " style="font-size: 26rpx;">
							{{item.dc_starttime_text}}~{{item.dc_endtime_text}}</view>
					</view>
					<view class="flexbetween margin_top1" v-if="item.orderinfo && item.orderinfo.pay_image">
						<view class="xiaohei main_size">支付憑證</view>
						<image class="listleftimg1 radius" :src="item.orderinfo.pay_image_text" mode=""></image>
					</view>
					<view class="flexright margin_top1" v-if="item.orderinfo && item.orderinfo.status==1">
						<view class="listbtnhuang"
							@click.stop="pzshow=true;currentid=item.id;">上傳憑證</view>
					</view>
				</view>
			</template>
			<!-- 領取 -->
			<template v-if="tabsid==4">
				<view class="mainpadding_top radius shadow dingwei" style="background-color: #fbf6f1;" v-for="item in list.data">
					<!-- 状态:1=待支付,2=待领取,3=已领取,4=已作废 -->
					<view class="checkfocus xiaobai" style="background-color: #ff0000;" v-show="item.status==1">待支付</view>
					<view class="checkfocus xiaobai" style="background-color: #ffb800;" v-show="item.status==2">待領取</view>
					<view class="checkfocus xiaobai" style="background-color: #06c763;" v-show="item.status==3">已領取</view>
					<view class="checkfocus xiaobai" style="background-color: #999999;" v-show="item.status==4">已作廢</view>
					<view class="flexbetween pdl" style="padding-bottom: 10rpx;">
						<view class="flexleft ">
							<view class="margin_right2">
								<image :src="item.image_text" mode="" class="radius"
									style="width: 120rpx;height: 120rpx;min-width: 120rpx;"></image>
							</view>
							<view class="line margin_right2"></view>
							<view>
								<view class="hei_text titletext bold" style="word-break: break-all;">{{item.name}}</view>
								<view class="margin_top1">
									<view class="huang_text main_size bold " v-show="item.payinfo.paytype==1">
										點數支付
									</view>
									<view class="huang_text main_size bold " v-show="item.payinfo.paytype==2">多元支付
									</view>
									<view class="huang_text main_size bold " v-show="item.payinfo.paytype==3">現金支付
									</view>
									<view class="huang_text main_size bold " v-show="item.payinfo.paytype==4">信用卡
									</view>
									<view class="huang_text main_size bold " v-show="item.payinfo.paytype==5">Line Pay線上支付
									</view>
									<view class="huang_text main_size bold " v-show="item.payinfo.paytype==6">街口線上支付
									</view>
									<view class="huang_text main_size bold " v-show="item.payinfo.paytype==7">街口線下支付
									</view>
									<view class="huang_text main_size bold " v-show="item.payinfo.paytype==8">Line Pay線下支付
									</view>
									<view class="huang_text main_size bold " v-show="item.payinfo.paytype==9">Line Pay二維碼支付
									</view>
									<view class="huang_text main_size bold " v-show="item.payinfo.paytype==10">街口二維碼支付
									</view>
									<view class="huang_text main_size bold " v-show="item.payinfo.paytype==11">全支付線下支付
									</view>
								</view>
							</view>
						</view>
						<view class="use" @click="lingqu(item)" v-show="item.status==2 && item.get_starttime_text">領取</view>
					</view>
					<view class="flexleft margin_top1" v-if="item.get_starttime_text">
						<view class="hui_text bold" style="font-size: 26rpx;">領取時間</view>
						<view class="hong_text bold " style="font-size: 26rpx;">
							{{item.get_starttime_text}}~{{item.get_endtime_text}}</view>
					</view>
					<view class="margin_top1" v-if="item.status==2 && !item.get_starttime_text">
						<view class="hong_text bold " style="font-size: 26rpx;">等待領取時間，目前還不能領取哦~</view>
					</view>
				</view>
			</template>
		</view>
		<u-modal :show="scanshow" title="掃碼領券" confirmText="關閉"
			@confirm="scanshow=false" @cancel="scanshow=false" confirmColor="#ffb800">
			<view class="slot-content" style="width: 600rpx;">
				<view class="flexcenter" v-if="scanshow">
					<uqrcode ref="uqrcode" canvas-id="qrcode" v-bind:value="qrcodeurl_text" :options="{ margin: 10}"
						size="300">
					</uqrcode>
				</view>
			</view>
		</u-modal>
		<!-- 上傳支付憑證 -->
		<u-modal :show="pzshow" title="上傳憑證" confirmColor="#ffb800" confirmText="確認" @confirm="upconfirm"
			cancelText="取消" @cancel="pzshow=false" showCancelButton>
			<view class="slot-content" style="width: 730rpx;">
				<view class="flexcenter">
					<view class="itemimgBox flexcenter margin_top2" v-for="(item,index) in images_text" :key="index">
						<!-- 已上傳 -->
						<view class="listleftimg1 radius">
							<image class="radius" :src="item" mode=""></image>
							<view class="deleteimg" @click="delindex(index,images,images_text)"></view>
						</view>
					</view>
					<view class="itemimgBox flexcenter margin_top2" @click="uploadImg(1,images,images_text)"
						v-if="images.length<1"><!-- 未上傳 -->
						<image class="listleftimg1 radius" src="@/static/image/system/upimg.png" mode=""></image>
					</view>
				</view>
			</view>
		</u-modal>
		<tabbar ref="tabbarref" :current="2" ></tabbar>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import WebSocket from '@/common/websocket.js' //websocket封装
	export default {
		data() {
			return {
				page: 1,
				limit: 12,
				list: {
					data: []
				},
				user_id:"",
				chatList:[],
				tabsid: 0,
				tabList: [{
					name: "訊息",
					id: 0,
				},{
					name: "公告",
					id: 1,
				}, {
					name: "活動",
					id: 2,
				}, {
					name: "調查",
					id: 3,
				}, {
					name: "領取",
					id: 4,
				}],
				scanshow:false,
				qrcodeurl_text: "",
				pzshow:false,
				currentid:"",
				images:[],
				images_text:[],
			}
		},
		onLoad() {
			uni.hideTabBar()
			this.pubUrl = this.imgUrl
			uni.$on("updatechatFnteList", data => {
				this.initchat()
			})
			
		},
		onShow() {
			this.user_id = uni.getStorageSync("user_id")
			this.initchat()
			this.$nextTick(() => {
				this.$refs.tabbarref.init()
			});
		},
		onUnload() {
			uni.$off('updatechatFnteList')
		},
		methods: {
			init(isPage, page) {
				let _this = this
				let url = "/api/index/ptnewsIndex"
				let data = {
					page: page || 1,
					limit: _this.limit,
					type: this.tabsid
				}
				if(this.tabsid==0) return
				if (this.tabsid == 3) {
					url = "/api/Shopwelfarecl/getWelfareIndex"
					data = {
						page: page || 1,
						limit: _this.limit,
						shop_id: uni.getStorageSync("shop_id")
					}
				}
				if (this.tabsid == 4) {
					url = "/api/Shopwelfarecl/welfareclOrder"
					data = {
						page: page || 1,
						limit: _this.limit,
						shop_id: uni.getStorageSync("shop_id")
					}
				}
				httpRequest.request(url, 'GET', data).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			uploadImg(value, upImgs, upImgs_text) { //上傳圖片
				let _this = this;
				let num = Number(value) - upImgs.length;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相機', '相簿'],
					success: function(res) {
						// 選擇相機走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 選擇相簿走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			delindex(index, upImg, upImgs_text) {
				upImg.splice(index, 1)
				upImgs_text.splice(index, 1)
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				this.http.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
			upconfirm() {
				if (this.images.length == 0) {
					httpRequest.toast("請上傳圖片")
				}
				this.pzshow = false
				let url = "/api/Shopwelfarecl/updateOrderImage"
				let data = {
					id: this.currentid,
					pay_image: this.images.join()
				}
				httpRequest.request(url, 'GET', data).then(res => {
					httpRequest.toast(res.msg);
					if (res.code == 1) {
						this.images = []
						this.images_text = []
						this.init()
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			lingqu(item){
				this.qrcodeurl_text = "/pages/workbench/huodonghx?id="+item.id
				this.scanshow = true
			},
			tabchange(e) {
				this.tabsid = e.id
				this.init()
				this.list.data = []
			},
			goSystemNotice(type){ //系統通知
				uni.navigateTo({
					url:"/pages/message/systemNotice?type="+type
				})
			},
			initchat() {
				httpRequest.requestChat('/api/user/getChatList', 'GET', {
					user_id:uni.getStorageSync("user_id"),
					sendtype:2
				}).then(res => {
					this.chatList = res.data
				})
			},
			// 进入聊天
			enterchat(item) { //进入聊天
				let data = {
					firend_id: item.firend.id,
					friend_name: item.firend.nickname,
					firend_avatar_text: item.firend.avatar_text
				}
				uni.navigateTo({
					url: "/pages/message/chat?msg=" + encodeURIComponent(JSON.stringify(data))
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.imgBox {
		width: 80rpx;
		height: 80rpx;
	}
	.border_bottom{
		border-bottom: 2rpx solid rgba(0, 0, 0, 0.03);
	}
	.chatBox {
		background-color: #FAFAFA;
		padding: 70rpx 40rpx 0;
	}
	
	.chatItemMessage {
		display: flex;
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid rgba(0, 0, 0, .08);
		.imageBox {
			width: 80rpx;
			min-width: 80rpx;
			height: 80rpx;
			border-radius: 10rpx;
			overflow: hidden;
			margin-right: 20rpx;
	
			image {
				width: 100%;
				height: 100%;
			}
		}
	
		.right {
			flex: 1;
		}
	
		.nameTime {
			display: flex;
			justify-content: space-between;
	
			.name {
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #000000;
				margin-right: 10rpx;
			}
	
			.number {
				width: 34rpx;
				height: 34rpx;
				line-height: 34rpx;
				text-align: center;
				background: #FF526A;
				border-radius: 50%;
				font-size: 20rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
			}
	
			.time {
				font-size: 24rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #333;
			}
		}
	
		.bottom {
			font-size: 26rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #333;
		}
	}
	
	.stateBox {
		padding: 5rpx 15rpx;
	}
	
	.color2 {
		color: #fff;
		background: #9064ff;
	}
	.listleftimg1 {
		width: 150rpx;
		min-width: 150rpx;
		height: 150rpx;
		position: relative;
	
		image {
			width: 100%;
			height: 100%;
		}
	}
	.use {
		width: 90rpx;
		height: 49rpx;
		text-align: center;
		line-height: 49rpx;
		background: #ed4d2d;
		border-radius: 4rpx 4rpx 4rpx 4rpx;
		border: 1rpx solid #ed4d2d;
		font-size: 26rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #fff;
	}
	::v-deep .u-transition {
		z-index: 900 !important;
	}
</style>
